package com.lechi.web.framework.util;

import com.google.common.collect.*;
import com.lechi.web.system.domain.IotMonitorDataBo;
import lombok.Getter;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class GuavaCacheUtil {


    /**
     * 启动状态
     */
    @Getter
    public final static String STARTED = "1";


    /**
     * 关闭状态
     */
    @Getter
    public final static String STOPED = "0";

    @Getter
    private final static Map<String, Date> deviceLastDated = Maps.newConcurrentMap();

    public static String ifTimeOut(String deviceIdOrSN, int timeInternal) {
        return Optional.ofNullable(deviceLastDated.get(deviceIdOrSN))
                .map(endTime -> new Date().getTime() - endTime.getTime() > timeInternal * 60000L ? getSTOPED() : getSTARTED()).orElse(getSTOPED());
    }

    /**
     * Guava的BiMap类提供了双向映射的功能，即可以通过键获取值，也可以通过值获取键。 bidirectional
     */
    @Getter
    private final static BiMap<String, String> biDeviceSnAndIPMap = HashBiMap.create();


    @Getter
    private final static BiMap<Long, String> biDeviceIdAndIPMap = HashBiMap.create();

    /**
     *Table类允许您创建一个类似于二维表格的映射，其中每个键都可以映射到多个值。
     *
     */
    @Getter
    private final static Table<String, String, String> deviceTableMap = HashBasedTable.create();

    @Getter
    private final static Map<String, List<?>> cmdParseTableMap = Maps.newConcurrentMap();

    @Getter
    private final static Table<Long, Long, IotMonitorDataBo> monitorDataMap = HashBasedTable.create();

    /**
     * Multimap类允许您将多个值映射到单个键上,返回可能是数组 并使用put（）方法将多个值与单个键相关联
     */
    @Getter
    private final static ListMultimap<Long, String> multiValueMap = ArrayListMultimap.create();
}
